package com.haojiang.mh.remote.adapter


import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils
import com.haojiang.mh.remote.bean.AreaValue
import com.haojiang.mh.remote.bean.ControlLeftMenuBean
import com.haojiang.mh.remote.enums.EMassage
import com.haojiang.mh.remote.utils.BleUtil
import com.richmat.rmremote.widget.*


class MassageFragAdapter : RecyclerView.Adapter<MassageFragAdapter.MassageFragViewHolder>() {

    private val viewTypeList: MutableList<Int> = mutableListOf()
    private val dataMap: MutableMap<Int, MutableList<AreaValue>> = mutableMapOf()

//    private var iMotorItemClickListener: IMotortemClickListener? = null

//    fun setOnMotorItemClickListener(menuItemClickListener: IMotortemClickListener?) {
//        this.iMotorItemClickListener = menuItemClickListener
//    }

    fun setData(map: MutableMap<Int, MutableList<AreaValue>>) {
        ThreadUtils.ui {
            viewTypeList.clear()
            dataMap.clear()
            for ((key, value) in map) {
                viewTypeList.add(key)
                dataMap.put(key, value)
            }
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return viewTypeList.size
    }


    /**
     * 设置界面
     */
    override fun onBindViewHolder(holder: MassageFragViewHolder, position: Int) {
        val viewType = viewTypeList.get(position)
        val cmdList = dataMap.get(viewType)
//        LogUtil.e(cmdList.toString())
        if (cmdList != null) {
            BleUtil.traverseView(holder.itemView, cmdList)
        }

//        cmd.code?.let {
//            when (cmd.code) {
//                CmdKey.HEAD_UP,
//                CmdKey.HEAD_DOWN -> {
//                    val itemView = holder.itemView as HeadItemView
//                }
//
//                CmdKey.FOOT_UP,
//                CmdKey.FOOT_DOWN -> {
//                    val itemView = holder.itemView as FootItemView
//                }
//            }
//        }


//        if (dataList.get(position).isClick) {
//            itemView.setData(data, true)
//        } else {
//            itemView.setData(data, false)
//        }

//        itemView.setOnClickListener(object : View.OnClickListener {
//            override fun onClick(p0: View?) {
//                for (i in 0 until dataList.size) {
//                    dataList.get(i).isClick = false
//                }
//                dataList.get(position).isClick = true
//                notifyDataSetChanged()
////                iMotorItemClickListener?.onItemClick(dataList.get(position))
//            }
//        })
    }

    override fun getItemViewType(position: Int): Int {
        return viewTypeList.get(position)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MassageFragViewHolder {
        when (viewType) {
            EMassage.HEAD_INTENSITY_INC.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootIntensityIncItemView(parent.context)
                )
            }

            EMassage.HEAD_INTENSITY_INC2.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootIntensityInc2ItemView(parent.context)
                )
            }

            EMassage.FOOT_INTENSITY_INC.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootIntensityDecItemView(parent.context)
                )
            }

            EMassage.HEAD_FOOT_BOTH_INTENSITY.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootBothIntensityItemView(parent.context)
                )
            }


            EMassage.HEAD_FOOT_BOTH_SPEED.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootBothSpeedItemView(parent.context)
                )
            }

            EMassage.MODE.key -> {
                return MassageFragViewHolder(
                    MsgModetemView(parent.context)
                )
            }

            EMassage.MODE2.key -> {
                return MassageFragViewHolder(
                    MsgMode2temView(parent.context)
                )
            }

            EMassage.AROMA_ON_OFF.key -> {
//                LogUtil.e("AROMA_ON_OFF")
                return MassageFragViewHolder(
                    MsgAromaItemView(parent.context)
                )
            }

            EMassage.WAVE.key -> {
                return MassageFragViewHolder(
                    MsgWaveItemView(parent.context)
                )
            }

//            ITEM_UBL_ON_OFF -> {
//                return MassageFragViewHolder(
//                    UblOnOffItemView(parent.context)
//                )
//            }
//
//            ITEM_UBL_OFF -> {
//                return MassageFragViewHolder(
//                    UblOffItemView(parent.context)
//                )
//            }




            EMassage.MIN.key -> {
                return MassageFragViewHolder(
                    MsgMinItemView(parent.context)
                )
            }

            EMassage.INTENSITY.key -> {
                return MassageFragViewHolder(
                    MsgIntensityItemView(parent.context)
                )
            }

            EMassage.HEAD_FOOT_BOTH_ON.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootBothOnItemView(parent.context)
                )
            }

            EMassage.HEAD_FOOT_BOTH_OFF.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootBothOffItemView(parent.context)
                )
            }

            EMassage.HEAD_FOOT_ON_OFF.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootOnOffItemView(parent.context)
                )
            }

            EMassage.MSG_OFF.key -> {
                return MassageFragViewHolder(
                    MsgHeadFootBothOff2ItemView(parent.context)
                )
            }

        }
        return MassageFragViewHolder(
            MsgHeadFootIntensityIncItemView(parent.context)
        )
    }


    /**
     * adapter的ViewHolder类
     */
    class MassageFragViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}


    interface IMotortemClickListener {
        fun onItemClick(
            homeMenuItemBean: ControlLeftMenuBean
        )
    }
}
